package cn.iocoder.yudao.module.team.controller.admin.fleet;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.team.controller.admin.fleet.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.fleet.FleetDO;
import cn.iocoder.yudao.module.team.service.fleet.FleetService;

@Tag(name = "管理后台 - 团队管理-车队信息")
@RestController
@RequestMapping("/team/fleet")
@Validated
public class FleetController {

    @Resource
    private FleetService fleetService;

    @PostMapping("/create")
    @Operation(summary = "创建团队管理-车队信息")
    @PreAuthorize("@ss.hasPermission('team:fleet:create')")
    public CommonResult<Integer> createFleet(@Valid @RequestBody FleetSaveReqVO createReqVO) {
        return success(fleetService.createFleet(createReqVO));
    }

    @PostMapping("/createBatch")
    @Operation(summary = "批量创建团队管理-车队信息")
    @PreAuthorize("@ss.hasPermission('team:fleet:create')")
    public CommonResult<Boolean> createBatch(@Valid @RequestBody List<FleetDO>  createReqVO) {
        fleetService.createBatch(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新团队管理-车队信息")
    @PreAuthorize("@ss.hasPermission('team:fleet:update')")
    public CommonResult<Boolean> updateFleet(@Valid @RequestBody FleetSaveReqVO updateReqVO) {
        fleetService.updateFleet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除团队管理-车队信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('team:fleet:delete')")
    public CommonResult<Boolean> deleteFleet(@RequestParam("id") Integer id) {
        fleetService.deleteFleet(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除团队管理-车队信息")
                @PreAuthorize("@ss.hasPermission('team:fleet:delete')")
    public CommonResult<Boolean> deleteFleetList(@RequestParam("ids") List<Integer> ids) {
        fleetService.deleteFleetListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得团队管理-车队信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('team:fleet:query')")
    public CommonResult<FleetRespVO> getFleet(@RequestParam("id") Integer id) {
        FleetDO fleet = fleetService.getFleet(id);
        return success(BeanUtils.toBean(fleet, FleetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得团队管理-车队信息分页")
    @PreAuthorize("@ss.hasPermission('team:fleet:query')")
    public CommonResult<PageResult<FleetRespVO>> getFleetPage(@Valid FleetPageReqVO pageReqVO) {
        PageResult<FleetDO> pageResult = fleetService.getFleetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FleetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出团队管理-车队信息 Excel")
    @PreAuthorize("@ss.hasPermission('team:fleet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFleetExcel(@Valid FleetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FleetDO> list = fleetService.getFleetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "团队管理-车队信息.xls", "数据", FleetRespVO.class,
                        BeanUtils.toBean(list, FleetRespVO.class));
    }

}